//
//  HomeHeaderUPView.m
//  FD
//
//  Created by Yasin on 2018/10/12.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "HomeHeaderUPView.h"
#import "MineModel.h" //11-13复用
@interface HomeHeaderUPView()
//消息按钮
@property (weak, nonatomic) IBOutlet UIButton *msgButton;
/**小圆点默认隐藏*/
@property (nonatomic, strong) UIView *redDotView;
/**加一个透明按钮扩大点击范围*/
@property (nonatomic, strong) UIButton *bigMsgTransparencyBtn;
/**2019-1-15添加透明按钮扩大点击范围*/
@property (nonatomic, strong) UIButton *leftTopTransparentBigBtn;
/**2019-1-21头像 点击头像跳转到个人资料页面 xib中打开用户交互,然后添加手势
 2019-2-19 点击昵称同上
 */
@end

@implementation HomeHeaderUPView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    //更新按钮文字的状态:左边文字，右边图片
    [self.addressBtn leftTextRightImageWithMargin:10];
    
    //切圆角
    self.iconIv.layer.masksToBounds = YES;
    self.iconIv.layer.cornerRadius = 30;
    
    //国际化适配11-7
    NSString *jifenStr = NSLocalizedString(My_jiFen, @"我的V积分");
    NSString *kaQuanStr = NSLocalizedString(My_kaQuan, @"我的卡券");
    
    self.jiFenLb.text = jifenStr;
    self.kaQuanLb.text = kaQuanStr;
    
    NSString *ckpsfwStr = NSLocalizedString(L_Home_LookRange, @"查看配送范围");
    [self.addressBtn setTitle:ckpsfwStr forState:(UIControlStateNormal)];
    [self.addressBtn leftTextRightImageWithMargin:5]; //1-9略微右移左文字右边图片
    
    
    [self.msgButton addSubview:self.redDotView];//圆点
    self.redDotView.hidden = YES;
    [self addSubview:self.bigMsgTransparencyBtn];
    
    
    
    //2019-1-21头像添加点击手势 点击跳转个人资料页面
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickIconIvTapGesAction)];
    [self.iconIv addGestureRecognizer:tapGes];

//    [self.phoneLb addGestureRecognizer:tapGes];
    UITapGestureRecognizer *tapPhoneGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickIconIvTapGesAction)];
    [self.phoneLb addGestureRecognizer:tapPhoneGes];
}


- (void)layoutSubviews {
    [super layoutSubviews];
    [self.redDotView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.msgButton.mas_right).offset(-5);
        make.top.equalTo(self.msgButton.mas_top);
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    
    [self.bigMsgTransparencyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right);
        make.top.equalTo(self.mas_top);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
}


#pragma mark - 点击事件
//点击定位
- (IBAction)clickLocationAction:(UIButton *)sender {
    
    __weak typeof(self) weakSelf = self;
    
    if (weakSelf.homeHeaderUpViewClickLocationBlock) {
        weakSelf.homeHeaderUpViewClickLocationBlock();
    }
}
//点击消息
- (IBAction)clickMsgAction:(UIButton *)sender {
    
    __weak typeof(self) weakSelf = self;

    if (weakSelf.homeHeaderUpViewClickYuYuBlock) {
        weakSelf.homeHeaderUpViewClickYuYuBlock();
    }
    
    [self hiddenRedDotView];
}

//点击头像手势
- (void)clickIconIvTapGesAction {
    
    if (self.homeHeaderUpViewClickAvatorBlock) {
        self.homeHeaderUpViewClickAvatorBlock();
    }
}



//!!!!:2019-2-15需求变更, 废弃 请使用：updateHomeUserDataWithDic
/**请求头像电话、昵称等数据,并进行赋值
 */
- (void)requestHomeUserDataAndAssignData {
    [FDHttpAPi requestHomeSuccessBlock:^(id responseObjc) {
        NSString *event = responseObjc[@"event"];
        
        if ([event isEqualToString:SUCCESS]) {
            //解析数据
            NSDictionary *dataDic = responseObjc[@"data"][@"user"];
            MineModel *model = [MineModel mj_objectWithKeyValues:dataDic];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                // 判断如果user.id=-1，则表示当前用户未登录， 昵称显示“未登录”，头像显示缺省头像
                NSNumber * userId = dataDic[@"id"];
                if ([userId isEqualToNumber:@(-1)]) {
                    self.phoneLb.text = NSLocalizedString(L_WeiDengLu, @"未登录");
                    self.iconIv.image = [UIImage imageNamed:@"默认头像"];
                }else {
                    self.phoneLb.text = model.nickname;
                    [self.iconIv sd_setImageWithURL:[NSURL URLWithString:model.head] placeholderImage:[UIImage imageNamed:@"默认头像"]];
                }
                
                
                //国际化适配11-7
                NSString *jifenStr = NSLocalizedString(My_jiFen, @"couponV积分");
                NSString *kaQuanStr = NSLocalizedString(My_kaQuan, @"我的卡券");

                if ([model.bonus isEqualToNumber:@(0)]) {
                    self.jiFenLb.text = [NSString stringWithFormat:@"%@:%.1f",jifenStr,[model.bonus floatValue]];
                }else {
                    self.jiFenLb.text = [NSString stringWithFormat:@"%@:%@",jifenStr,model.bonus];
                }
                
                self.kaQuanLb.text = [NSString stringWithFormat:@"%@:%@",kaQuanStr,model.coupon];
            });
        }else {
            [JRToast showWithText:responseObjc[describe]];
        }
        
    } failBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
    }];
}




//- (void)requestHomeUserDataAndAssignData {
//
//    [FDHttpAPi requestUserMySuccessBlock:^(id responseObjc) {
//        NSString *event = responseObjc[@"event"];
//        if ([event isEqualToString:SUCCESS]) {
//            //解析数据
//            NSDictionary *dataDic = responseObjc[@"data"];
//            MineModel *model = [MineModel mj_objectWithKeyValues:dataDic];
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [self.iconIv sd_setImageWithURL:[NSURL URLWithString:model.head] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
//                self.phoneLb.text = model.nickname;
//
//
//                //国际化适配11-7
//                NSString *jifenStr = NSLocalizedString(My_jiFen, @"couponV积分");
//                NSString *kaQuanStr = NSLocalizedString(My_kaQuan, @"我的卡券");
//
//
//
//                self.jiFenLb.text = [NSString stringWithFormat:@"%@:%.1f",jifenStr,[model.bonus floatValue]];
//                self.kaQuanLb.text = [NSString stringWithFormat:@"%@:%@",kaQuanStr,model.coupon];
//            });
//        }else {
//            [JRToast showWithText:responseObjc[describe]];
//        }
//    } failBlock:^(NSError *error) {
//        FDLog(@"%@",[error localizedDescription]);
//    }];
//}

#pragma mark - getter
- (UIView *)redDotView {
    if (!_redDotView) {
        _redDotView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        _redDotView.backgroundColor = [UIColor redColor];
        _redDotView.layer.masksToBounds = YES;
        _redDotView.layer.cornerRadius = 5;
    }
    return _redDotView;
}
- (UIButton *)bigMsgTransparencyBtn {
    if (!_bigMsgTransparencyBtn) {
        _bigMsgTransparencyBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_bigMsgTransparencyBtn addTarget:self action:@selector(clickMsgAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _bigMsgTransparencyBtn;
}

#pragma mark - 接口显示隐藏小圆点
//显示隐藏小圆点
- (void)showRedDotView {
    self.redDotView.hidden = NO;
}

- (void)hiddenRedDotView {
    self.redDotView.hidden = YES;
}

@end
